package com.dragonsky.shop.order.service.impl;

import com.dragonsky.shop.common.utils.AjaxResult;
import com.dragonsky.shop.order.config.OrderNaCosConfig;
import com.dragonsky.shop.order.domain.vo.OrderNaCosVo;
import com.dragonsky.shop.order.entity.Order;
import com.dragonsky.shop.order.mapper.OrderMapper;
import com.dragonsky.shop.order.service.OrderItemService;
import com.dragonsky.shop.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Random;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author dragon
 * @since 2025-03-22 00:18:05
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private OrderNaCosConfig orderNaCosConfig;

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderItemService orderItemService;


    @Override
    public AjaxResult<OrderNaCosVo> getNaCosConfig() {
        return AjaxResult.succeedWith(OrderNaCosVo
            .builder()
            .orderName(orderNaCosConfig.getOrderName())
            .orderPrice(orderNaCosConfig.getOrderPrice())
            .orderNum(orderNaCosConfig.getOrderNum())
            .build()
        );
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public AjaxResult<Void> seata() {
        orderMapper.insert(Order
            .builder()
            .orderNo("seata order")
            .userId(1L)
            .status(Byte.parseByte("0"))
            .orderPrice("99.99")
            .createTime(LocalDateTime.now())
            .updateTime(LocalDateTime.now())
            .build()
        );

        Random random = new Random();
        int i2 = random.nextInt(10) + 1;
        if (i2 > 5) {
            throw new RuntimeException("订单数据需要回滚");
        }

        return AjaxResult.succeed();
    }

}
